package top.z.work.module.system.controller.config;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.z.work.framework.common.pojo.PageInfo;
import top.z.work.framework.common.pojo.R;
import top.z.work.module.system.controller.config.vo.SystemConfigCreateVO;
import top.z.work.module.system.controller.config.vo.SystemConfigModifyVO;
import top.z.work.module.system.controller.config.vo.SystemConfigVO;
import top.z.work.module.system.convert.SystemConfigConvert;
import top.z.work.module.system.entity.SystemConfig;
import top.z.work.module.system.service.SystemConfigService;

import java.util.List;

/**
 * 配置管理Controller
 *
 * @author zaoangod
 * @since 2024-05-10 16:38:44
 */
@Tag(name = "配置管理")
@Slf4j
@Validated
@RestController
@RequestMapping("system/config")
public class SystemConfigController {

    @Resource
    private SystemConfigService systemConfigService;

    @Operation(summary = "配置分页", description = "获取配置分页")
    @GetMapping("page")
    public R<PageInfo<SystemConfigVO>> list(
            SystemConfigVO parameter,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(name = "size", defaultValue = "10") Integer size
    ) {
        QueryWrapper       query    = SystemConfigConvert.INSTANCE.convert(parameter);
        Page<SystemConfig> data     = Page.of(page, size);
        Page<SystemConfig> pageInfo = systemConfigService.page(data, query);
        return R.s(SystemConfigConvert.INSTANCE.convert(pageInfo));
    }

    @Operation(summary = "配置列表", description = "获取配置列表")
    @GetMapping("list")
    public R<List<SystemConfigVO>> queryList(SystemConfigVO parameter) {
        QueryWrapper       query = SystemConfigConvert.INSTANCE.convert(parameter);
        List<SystemConfig> list  = systemConfigService.list(query);
        return R.s(SystemConfigConvert.INSTANCE.convert(list));
    }

    @Operation(summary = "配置信息", description = "获取配置信息")
    @GetMapping("info")
    public R<SystemConfigVO> queryOne(SystemConfigVO parameter) {
        QueryWrapper query        = SystemConfigConvert.INSTANCE.convert(parameter);
        SystemConfig systemConfig = systemConfigService.getOne(query);
        return R.s(SystemConfigConvert.INSTANCE.convert(systemConfig));
    }

    @Operation(summary = "删除配置", description = "根据配置ID删除数据")
    @DeleteMapping("remove")
    public R<Boolean> remove(@RequestParam("identity") @NotNull(message = "数据主键不能为空") Long identity) {
        boolean success = systemConfigService.removeById(identity);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "修改配置", description = "根据配置ID修改数据")
    @PutMapping("modify")
    public R<Boolean> modify(@RequestBody SystemConfigModifyVO parameter) {
        SystemConfig config  = SystemConfigConvert.INSTANCE.convert(parameter);
        boolean      success = systemConfigService.updateById(config);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "添加配置", description = "添加配置数据")
    @PostMapping("add")
    public R<Boolean> add(@RequestBody SystemConfigCreateVO parameter) {
        SystemConfig config  = SystemConfigConvert.INSTANCE.convert(parameter);
        boolean      success = systemConfigService.save(config);
        return success ? R.s(true) : R.f();
    }

    @Operation(summary = "批量删除", description = "批量删除配置数据")
    @PostMapping("batch-remove")
    public R<Boolean> batchRemove(
            @RequestBody
            @NotNull(message = "数据主键列表不能为空")
            @NotEmpty(message = "数据主键列表不能为空")
            List<String> identityList
    ) {
        boolean success = systemConfigService.removeByIds(identityList);
        return success ? R.s(true) : R.f();
    }

}
